<!-- AnnouncementList.vue -->
<script setup>
import { onMounted, ref } from "vue";
import { Delete, Edit, Plus } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { announcementList, insertAnnouncement, deleteAnnouncement, updateAnnouncement } from "@/api/announcement";

// 分页条数据模型
const pageNo = ref(1); // 当前页
const total = ref(0); // 总条数
const pageSize = ref(5); // 每页条数
// 搜索对象
const searchParams = ref({
  title: '',
  publishDate: ''
});
// 表格对象
const announcementListData = ref([]);
const form = ref(null);
const titleName = ref('');
// 添加表单对象
const formData = ref({
  id: '',
  title: '',
  content: '',
  publishDate: ''
});
const drawerFormVisible = ref(false);

// 添加信息公示
const showAddDialog = (title) => {
  titleName.value = title;
  drawerFormVisible.value = true;

};

onMounted(() => {
  getAnnouncementList();
});

// 查询信息公示
const getAnnouncementList = () => {
  const params = {
    params: {
      pageNo: pageNo.value,
      pageSize: pageSize.value,
      ...searchParams.value
    }
  };
  announcementList(params).then(res => {
    if (res.success) {
      total.value = res.data.count;
      announcementListData.value = res.data.records;
    }
  });
};

// 重置
const resetParams = () => {
  searchParams.value = {
    title: '',
    publishDate: ''
  };
  getAnnouncementList();
};

// 编辑信息公示
const showEditDialog = (row, title) => {
  formData.value = { ...row };
  titleName.value = title;
  drawerFormVisible.value = true;
};

// 删除信息公示
const deleteById = (id) => {
  ElMessageBox.confirm(
      '确认删除吗，删除后不可恢复?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    deleteAnnouncement({ id: id }).then(res => {
      if (res.success) {
        ElMessage.success(res.message);
        getAnnouncementList();
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '取消删除',
    });
  });
};

// 当每页条数发生了变化，会调用
const handleSizeChange = (val) => {
  pageSize.value = val;
  getAnnouncementList();
};

// 当当前页面发生变化，会调用
const handleCurrentChange = (val) => {
  pageNo.value = val;
  getAnnouncementList();
};

// 表单校验
const rules = {
  title: [
    { required: true, message: '请输入标题', trigger: 'blur' },
    { min: 3, max: 50, message: '长度为3-50位', trigger: 'blur' }
  ],
  content: [
    { required: true, message: '请输入内容', trigger: 'blur' }
  ],
  publishDate: [
    { required: true, message: '请选择发布日期', trigger: 'change' }
  ]
};

function cancelClick(formEl) {
  formEl.resetFields();
  drawerFormVisible.value = false;
}

// 添加信息公示发送请求
const addAnnouncement = () => {
  form.value.validate((valid) => {
    if (valid) {
      insertAnnouncement(formData.value).then(res => {
        if (res.success) {
          ElMessage.success(res.message);
          drawerFormVisible.value = false;
          getAnnouncementList();
        }
      }).catch(error => {
        ElMessage.error('提交失败：' + error.message);
      });
    }
  });
};

// 编辑信息公示发送请求
const editAnnouncement = () => {
  form.value.validate((valid) => {
    if (valid) {
      updateAnnouncement(formData.value).then(res => {
        if (res.success) {
          ElMessage.success(res.message);
          drawerFormVisible.value = false;
          getAnnouncementList();
        }
      }).catch(error => {
        ElMessage.error('更新失败：' + error.message);
      });
    }
  });
};
</script>

<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>信息公示</span>
        <div class="extra">
          <el-button type="primary" @click="showAddDialog('添加信息公示')">添加信息公示</el-button>
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="标题：">
        <el-input v-model="searchParams.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="发布日期：">
        <el-date-picker
            v-model="searchParams.publishDate"
            type="date"
            placeholder="请选择时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getAnnouncementList">搜索</el-button>
        <el-button @click="resetParams">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 信息公示列表 -->
    <el-table :data="announcementListData" style="width: 100%">
      <el-table-column label="标题" prop="title"></el-table-column>
      <el-table-column label="内容" prop="content"></el-table-column>
      <el-table-column label="发布日期" prop="createTimeStr"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showEditDialog(row, '编辑信息公示')"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deleteById(row.id)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"/>
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[5, 10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange" style="margin-top: 20px; justify-content: flex-end"/>
  </el-card>
  <!--  添加信息公示抽屉-->
  <el-drawer v-model="drawerFormVisible" direction="rtl">
    <template #header>
      <h4>{{titleName}}</h4>
    </template>
    <template #default>
      <el-form :model="formData" ref="form" :rules="rules" label-position="top">
        <el-form-item label="标题" prop="title">
          <el-input autocomplete="off" v-model="formData.title" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea" autocomplete="off" v-model="formData.content" />
        </el-form-item>
        <el-form-item label="发布日期" prop="publishDate">
          <el-date-picker
              v-model="formData.publishDate"
              type="date"
              placeholder="请选择发布日期"
              format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div style="flex: auto">
        <el-button @click="cancelClick(form)">取消</el-button>
        <el-button type="primary" @click="titleName === '添加信息公示' ? addAnnouncement() : editAnnouncement()">确认</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<style scoped lang="scss">
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>